<?php
require_once 'UserDAL.php';
require_once 'DBConnection.php';
class LoginHandler {
	private $SessionLoginCheck = '';
	private $SessionLoginUser = '';
	public function isLoggedIn() {
		//Returns true if user is Logged in
		if (isset($_SESSION[$this -> SessionLoginCheck])) {
			return true;
		} else {
			return false;
		}
	}

	public function doLogin($name, $password) {
		//If fields are not empty, check input against "registered" users
		$dbc = new DBConnection();
		$dbc -> connect();
		$userDAL = new UserDAL($dbc);
		// Check if username exists
		if ($name != null && $password != null) {
			if ($userDAL -> userExists($name)) {
				// If username exists check if password matches
				if ($userDAL -> checkPassword($name, $password)) {
					// If password matches - log in
					$_SESSION[$this -> SessionLoginCheck] = "loggedIn";
					return true;
				} else {
					Feedback::addError(Feedback::wrongLogin);
				}
			} else {
				Feedback::addError(Feedback::wrongLogin);
			}
		}
	}

	public function doLogout() {
		// unset session variable
		unset($_SESSION[$this -> SessionLoginCheck]);
	}

}
?>